package com.example.demo2.service.impl;

import java.util.Random;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo2.common.utils.Dates;
import com.example.demo2.common.utils.Strings2;
import com.example.demo2.entity.AccountExtend;
import com.example.demo2.mapper.AccountExtendMapper;
import com.example.demo2.service.AccountExtendService;

/**
 * @Author 胡亚伟
 * @Date 2025-06-10
 * @version 1.0
 * @注释
 */

@Service
public class AccountExtendServiceImpl extends ServiceImpl<AccountExtendMapper, AccountExtend>
        implements AccountExtendService {

    @Resource
    private AccountExtendMapper accountExtendMapper;

    public AccountExtend findByQq(String qq) {
        return accountExtendMapper.findAccountExtendByQq(qq);
    }

    public AccountExtend findByAccount(String account) {
        return accountExtendMapper.findAccountExtendByAccount(account);
    }

    public void bindQq(String qq) {
        AccountExtend ae = new AccountExtend();
        ae = new AccountExtend();
        ae.setSign(0);
        ae.setCoupon(0);
        ae.setExperience(0);
        ae.setConsume(0);
        ae.setQq(qq);
        ae.setCanbindaccount(1);
        ae.setCanbindqq(0);
        save(ae);
    }

    public void updateQq(AccountExtend ae, String qq) {
        ae.setQq(qq);
        ae.setCanbindqq(0);
        updateById(ae);
    }

    public void updateCanBindQq(AccountExtend ae) {
        ae.setCanbindqq(1);
        updateById(ae);
    }

    public void updateCanBindAccount(AccountExtend ae) {
        ae.setCanbindaccount(1);
        updateById(ae);
    }

    public void bindAccount(AccountExtend ae, String account) {
        ae.setAccount(account);
        ae.setCanbindaccount(0);
        updateById(ae);
    }

    /* 0用户不存在 1积分不足 2猜错 3猜对, magnification倍率， prefix 1 布 2剪刀 3 石头 */
    public int cq(AccountExtend ae, int magnification, int prefix) {
        int sign = ae.getSign();
        int consume = ae.getConsume();
        int random = new Random().nextInt(100);
        if ((magnification == 1 || magnification == 4) && sign < 10) {
            return 1;
        } else if (magnification == 2 && sign < 20) {
            return 1;
        } else if (magnification == 3 && sign < 30) {
            return 1;
        }

        int score = magnification * 10;

        if (magnification == 4) {
            score = ((int) (sign / 10)) * 10;
        }

        int result = 2;
        if (prefix == 1 && random < 33) {
            result = 3;
            ae.setSign(sign + score);
        } else if (prefix == 2 && random < 66 && random >= 33) {
            result = 3;
            ae.setSign(sign + score);
        } else if (prefix == 3 && random >= 66 && random < 100) {
            result = 3;
            ae.setSign(sign + score);
        } else {
            ae.setSign(sign - score);
            ae.setConsume(consume + score);
        }

        updateById(ae);
        return result;
    }

    // 1未绑定qq 0已签到 2签到成功 3两倍签到 4三倍 5五倍
    public int sign(AccountExtend a) {
        int result = 0;
        String time = Dates.format_yyyyMMdd(a.getSignTime());
        String now = Dates.format_yyyyMMdd(DateTime.now().toDate());
        if (Strings2.isNullOrWhiteSpace(time) || (!Strings2.isNullOrWhiteSpace(time) && !time.equals(now))) {

            int random = new Random().nextInt(100);

            int base = 15;
            result = 1;

            if (random == 0) {
                base = base * 5;
                result = 4;
            } else if (random > 0 && random < 15) {
                base = base * 3;
                result = 3;
            } else if (random > 14 && random < 35) {
                base = base * 2;
                result = 2;
            }

            a.setSign(base + a.getSign());
            a.setSignTime(DateTime.now().toDate());
        }
        updateById(a);
        return result;
    }

    public AccountExtend updateExperience(AccountExtend a, int count) {
        a.setExperience(count);
        updateById(a);
        return findByAccount(a.getAccount());
    }

    public AccountExtend updateSign(AccountExtend a, int count) {
        a.setSign(count);
        updateById(a);
        return findByAccount(a.getAccount());
    }

    public AccountExtend updateConsume(AccountExtend a, int count) {
        a.setConsume(count);
        a.setExperience(a.getExperience() + count);
        updateById(a);
        return findByAccount(a.getAccount());
    }

    public AccountExtend updateCoupon(AccountExtend a, int count) {
        a.setCoupon(count);
        updateById(a);
        return findByAccount(a.getAccount());
    }

    public AccountExtend rechargeSign(AccountExtend a, int count) {
        a.setSign(a.getSign() + count);
        updateById(a);
        return findByAccount(a.getAccount());
    }

    public AccountExtend rechargeConsume(AccountExtend a, int count) {
        a.setConsume(a.getConsume() + count);
        a.setExperience(a.getExperience() + count);
        updateById(a);
        return findByAccount(a.getAccount());
    }

    public AccountExtend rechargeCoupon(AccountExtend a, int count) {
        a.setCoupon(a.getCoupon() + count);
        updateById(a);
        return findByAccount(a.getAccount());
    }
}